﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NxPartyAffairsOpen.Models;
using NxPartyAffairsOpen.Helpers;

namespace NxPartyAffairsOpen.Controllers
{
    public class HomeController : Controller
    {
        private NXDBDataContext db = new NXDBDataContext();

        public ActionResult Index()
        {
            int count = 0;
            CategoryInfoModel gg = new CategoryInfoModel();
            gg.CategoryID = Config.DangWuGongGaoID;
            gg.DisplayName = "党务公告";
            gg.InformationList = CategoryHelper.GetInfoList(Config.AppID, Config.DangWuGongGaoID, 8, 0, "ShowTime", ref count, string.Empty, false, false, 1);
            ViewData["dangwugonggao"] = gg;
            count = 0;
            CategoryInfoModel dongtai = new CategoryInfoModel();
            dongtai.CategoryID = Config.DangWuDongTaiID;
            dongtai.DisplayName = "党务动态";
            dongtai.InformationList = CategoryHelper.GetInfoList(Config.AppID, Config.DangWuDongTaiID, 8, 0, "ShowTime", ref count, string.Empty, false, false, 1);
            ViewData["dangwudongtai"] = dongtai;

            PicNewModel pim = CategoryHelper.GetPicNews(Config.DangWuFengCaiID,  "pic1");
            pim.CategoryID = Config.DangWuFengCaiID;
            pim.DisplayName = "党务风采";


            ViewData["dangwufengcai"] = pim;


            return View();
        }

        public ActionResult Navigator(Guid? ID)
        {
            List<Category> cateList = db.Category.Where(l => l.Flag == "1" && l.IsJournal == "1" && l.AppID == Config.AppID && l.ParentID == (ID.HasValue ? ID.Value.ToString() : Guid.Empty.ToString())).OrderBy(o => o.OrderNum).ToList();
            List<Category> filterList = new List<Category>();
            foreach (Category item in cateList)
            {
                if (item.CategoryID != Config.DangWuDongTaiID && item.CategoryID != Config.DangWuFengCaiID && item.CategoryID != Config.DangWuGongGaoID)
                {
                    filterList.Add(item);
                }
            }

            ViewData["CategoryList"] = filterList;
            ViewData["DisplayName"] = "总目录";
            if (ID.HasValue)
            {
                Category cate = db.Category.SingleOrDefault(l => l.CategoryID == ID.Value);
                if (cate != null)
                    ViewData["DisplayName"] = cate.DisplayName;

            }


            if (cateList.Count > 0)
            {

                return View();

            }
            else
            {
                return RedirectToAction("InfoList", "Home", new { categoryid = (ID.HasValue ? ID.Value : Guid.Empty) });

            }


        }
        public ActionResult InfoList(Guid? categoryid, int? pi, int? year)
        {
            ViewData["DisplayName"] = "栏目";
            if (categoryid.HasValue)
            {
                Category cate = db.Category.SingleOrDefault(l => l.CategoryID == categoryid.Value);
                if (cate != null)
                    ViewData["DisplayName"] = cate.DisplayName;

            }

            Guid gd = categoryid.HasValue ? categoryid.Value : Guid.Empty;

            int count = 0;
            InformationListModel ilm = new InformationListModel();
            ilm.PageIndex = pi.HasValue ? pi.Value : 0;


            List<InformationModel> infoList = new List<InformationModel>();
            string where = string.Empty;
            ilm.Year = DateTime.Now.Year;
            if (year.HasValue)
            {
                ilm.Year = year.Value;
                if (ilm.Year < 1998 || year > 2200)
                {
                    ilm.Year = DateTime.Now.Year;
                }

            }
            where = "ShowTime>='" + ilm.Year + "-1-1 0:0:0' and ShowTime <='" + ilm.Year + "-12-31 0:0:0'";
            infoList = CategoryHelper.GetInfoList(Config.AppID, gd, 14, ilm.PageIndex, "ShowTime", ref count, where, true, true, 0);

            ilm.TotalCount = count;
            ilm.InformationList = infoList;

            return View(ilm);
        }
        public ActionResult Details(Guid? id)
        {
            InformationModel info = InformationHelper.GetInformation(db, id.HasValue ? id.Value : Guid.Empty);
            if (info == null)
                info = new InformationModel();

            return View(info);
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
